const mongoose = require('mongoose');

mongoose.connect('mongodb+srv://3129652273:liangyu521@cluster0.mjgyshm.mongodb.net/db_AIyunong')
  .then(() => { console.log('数据库连接成功'); })
  .catch(() => { console.log('数据库连接失败'); });

const userSchema = new mongoose.Schema({
  username: String,
  PersonId: String,
  password: String,
  email: String,
  refreshToken: String,
  refreshTokenExpires: Date,
  createdAt: Date,
  //用户信息
  target: String,
  gender: String,
  age: Number,
  bloodSign: String,
  bloodType: String,
  weight: Number,
  weightUnit: String,
});

const userModel = mongoose.model('user', userSchema, 'user');

module.exports={userModel,mongoose}

// 专业（Specialty）模型
const SpecialtySchema = new mongoose.Schema({

  name: { type: String, required: true }
});
const Specialty = mongoose.model('Specialty', SpecialtySchema);

// 医生（Doctor）模型
const DoctorSchema = new mongoose.Schema({
  name: { type: String, required: true },
  specialty: { type: mongoose.Schema.Types.ObjectId, ref: 'Specialty' },
  img: String,
  level: String,
  rating: Number,
  distance: String,
  desc: String
});
const Doctor = mongoose.model('Doctor', DoctorSchema);

// 聊天消息（ChatMessage）模型
const ChatMessageSchema = new mongoose.Schema({
  msgId: { type: String, required: true },
  from: { type: String, required: true },
  to: { type: String, required: true },
  text: { type: String },
  imgUrl: { type: String },
  audioUrl: { type: String }, // 新增音频URL字段
  audioText: { type: String }, // 新增音频转文字字段
  time: { type: String, required: true },
  date: { type: String, required: true }
});
const ChatMessage = mongoose.model('ChatMessage', ChatMessageSchema);

module.exports = {
  userModel,
  Specialty,
  Doctor,
  ChatMessage,
  mongoose
};
